package com.example.nuc_ms_beta.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.nuc_ms_beta.R;
import com.example.nuc_ms_beta.app.MyApplication;
import com.example.nuc_ms_beta.bean.activity.User;
import com.example.nuc_ms_beta.util.SharePreferencesUtil;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by bnhc on 17-6-3.
 * Complete User Message
 */

public class CompleteInformationActiviy extends Activity {
    private EditText activity_complete_username,activity_complete_userpass,activity_complete_useremail,activity_complete_phone;
    private Button activity_complete_ok_bt;
    private User user = new User();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_completelayout);
        initView();
    }

    private void initView() {
        activity_complete_username = (EditText) findViewById(R.id.activity_complete_username_tv);
        activity_complete_userpass = (EditText) findViewById(R.id.activity_complete_userpass_tv);
        activity_complete_useremail = (EditText) findViewById(R.id.activity_complete_useremail_tv);
        activity_complete_phone = (EditText) findViewById(R.id.activity_complete_phone_tv);
        activity_complete_phone.setText((String) SharePreferencesUtil.getData(MyApplication.context,"UserPhone","null"));
        activity_complete_ok_bt = (Button) findViewById(R.id.activity_complete_ok_bt);
        activity_complete_ok_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              saveUser();
            }
        });
    }

    private void saveUser(){
        user.setUsername(activity_complete_username.getText().toString());
        user.setPasswrod(activity_complete_userpass.getText().toString());
        user.setEmail(activity_complete_useremail.getText().toString());
        user.setMobilePhoneNumber(activity_complete_phone.getText().toString());
        user.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if(e==null){
                    SharePreferencesUtil.saveData(MyApplication.context,"objectId",s);
                    Toast.makeText(MyApplication.context,"注册成功",Toast.LENGTH_SHORT).show();
                    Intent loginIntent = new Intent(CompleteInformationActiviy.this,LoginActivity.class);
                    startActivity(loginIntent);
                    finish();
                }else{
                    Log.d("[Complet][SaveListener]",e.toString());
                    Toast.makeText(MyApplication.context,"注册失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
